package com.onepunch.common;

/**
 * @Author: yx
 * @Date: 2025/10/30 07:01
 * @Description:
 **/

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @projectName: onePunch
 * @package: com.onepunch.common
 * @className: DateUtils
 * @author: yx
 * @description:
 * @date: 2025/10/30 07:01
 * @version: 1.0
 */
public class DateUtils {
    // 统一日期格式
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * String 转 LocalDateTime
     */
    public static LocalDateTime strToLocalDateTime(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }
        return LocalDateTime.parse(dateStr, FORMATTER);
    }

    /**
     * LocalDateTime 转 String
     */
    public static String localDateTimeToStr(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.format(FORMATTER);
    }

    /**
     * 计算日期加天数
     */
    public static String plusDays(String dateStr, int days) {
        LocalDateTime dateTime = strToLocalDateTime(dateStr);
        if (dateTime == null) {
            return null;
        }
        return localDateTimeToStr(dateTime.plusDays(days));
    }

    /**
     * 计算日期减小时
     */
    public static String minusHours(String dateStr, int hours) {
        LocalDateTime dateTime = strToLocalDateTime(dateStr);
        if (dateTime == null) {
            return null;
        }
        return localDateTimeToStr(dateTime.minusHours(hours));
    }
}
